package com.link.base.base.statistics.controller;

import com.link.base.base.statistics.model.Statistics;
import com.link.base.base.statistics.service.StatisticsService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 存储知识库、论坛帖子、案例库等对象用户浏览、阅读、收藏、点赞记录
 *
 * @author ouyangzhenqiang
 * @date 2018/11/6
 */
@Controller
@RequestMapping("/link/Statistics")
public class StatisticsController {
    @Resource
    private StatisticsService statisticsService;

    /**
     * 记录浏览量和阅读人数
     *
     * @author ouyangzhenqiang
     * @date 2018/11/6
     */
    @RequestMapping(value = "/increasePageView", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> increasePageView(@RequestBody Statistics record, HttpServletRequest request,
                                                HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Statistics statistics = statisticsService.increasePageView(record);
            result.put("pageviews", statistics.getPageViewNum());
            result.put("readNum", statistics.getReadNum());
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 点赞/取消点赞
     *
     * @author ouyangzhenqiang
     * @date 2018/11/6
     */
    @RequestMapping(value = "/thumbOrCancel", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> thumbOrCancel(@RequestBody Statistics record, HttpServletRequest request,
                                               HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String num = statisticsService.dealDataForThumbAndCollect("thumb", record);
            result.put("num", num);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 收藏/取消收藏
     *
     * @author ouyangzhenqiang
     * @date 2018/11/6
     */
    @RequestMapping(value = "/collectOrCancel", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> collectOrCancel(@RequestBody Statistics record, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String num = statisticsService.dealDataForThumbAndCollect("collect", record);
            result.put("num", num);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

}
